import time
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

# 6个常用定位方式
'''
class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')

    # id 定位
    def id(self):
        # webelement就是所有通过selenium方法获取的标签对象
        # webelement.get_attribute(param)   #获取标签的属性
        # param: outerHTML   # 获取标签的源代码
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.get_attribute('outerHTML'))  # 打印标签源代码
        print(self.driver.find_element(By.ID, 'su').get_attribute('outerHTML'))

    # 通过标签的class属性定位元素 By.CLASS_NAME
    def class_name(self):
        print(self.driver.find_element(By.CLASS_NAME, 's_ipt').get_attribute('outerHTML'))

    # 通过链接文本定位元素
    def link_text(self):
        print(self.driver.find_element(By.LINK_TEXT, '新闻').get_attribute('outerHTML'))

    # 通过部分链接文本定位元素
    def partail_link_text(self):
        print(self.driver.find_element(By.PARTIAL_LINK_TEXT, 'hao').get_attribute('outerHTML'))

    # 通过标签名称定位元素
    def tag_name(self):
        # driver.find_element() 匹配到第一个标签
        print(self.driver.find_element(By.TAG_NAME, 'input').get_attribute('outerHTML'))
        # driver.find_elements() 匹配所有的标签，返回列表，可以通过下标定位
        ipt = self.driver.find_elements(By.TAG_NAME, 'input')
        print(len(ipt))
        print(ipt[2].get_attribute('outerHTML'))
        for e in ipt:
            print(e.get_attribute('outerHTML'))

    # 匹配不到会抛出异常
    def about_erro(self):
        # self.driver.find_element(By.ID, 'abc')  # selenium.common.exceptions.NoSuchElementException
        try:
            self.driver.find_element(By.ID, 'abc')
        except Exception as e:
            print(e)
        else:
            print(self.driver.find_element(By.ID, 'abc').get_attribute('outerHTML'))
        finally:
            print('hello')

    # 通过name属性定位元素
    def name(self):
        print(self.driver.find_element(By.NAME, 'wd').get_attribute('outerHTML'))

    # 关闭浏览器
    def quit(self):
        self.driver.quit()


b = TestCase()
b.id()
b.class_name()
b.link_text()
b.partail_link_text()
b.tag_name()
b.about_erro()
b.name()
b.quit()
'''

# 定位元素 xpath标签和属性
'''
kw = driver.find_element(By.XPATH, '//*[@id="kw"]')
print(kw.get_attribute('outerHTML'))

print(driver.find_element(By.XPATH, '//*[@id="su"]').get_attribute('outerHTML'))

ipt = driver.find_elements(By.XPATH, '//input')
print(len(ipt))
for i in ipt:
    print(i.get_attribute('outerHTML'))

# xpath层级定位
i1 = driver.find_element(By.XPATH, '//form/span/input')
print(i1.get_attribute('outerHTML'))

# XPATH 标签索引
# //form/input
print(driver.find_element(By.XPATH, '//form/input[2]').get_attribute('outerHTML'))

# XPATH 逻辑运算使用 and，or，not
print(driver.find_element(By.XPATH, '//input[@id="kw" and @class="s_ipt"]').get_attribute('outerHTML'))

# XPATH 模糊查询
hao = driver.find_element(By.XPATH, '//a[contains(text(), "hao")]')
print(hao.get_attribute('outerHTML'))

result = driver.find_element(By.XPATH, '//*[contains(@id, "result")]')
print(result.get_attribute('outerHTML'))

driver.quit()
'''

# 通过nth-child() 定位input
'''
ipt = driver.find_element(By.CSS_SELECTOR, '#form input:nth-child(11)')
print(ipt.get_attribute('outerHTML'))
'''

# webdriver常用属性
'''
# name 浏览器名称
print(driver.name)
# curren_url 网页连接
print(driver.current_url)
# page_source 网页源代码
# print(driver.page_source)
# current_window_handle 当前页句柄
print(driver.current_window_handle)
# window_handles  打开网页句柄
driver.find_element(By.LINK_TEXT, '新闻').click()
print(driver.window_handles)
'''
'''
# id标识
kw = driver.find_element(By.ID, 'kw')
print(kw.id)

# size宽高
print(kw.size)

# rect 宽高和坐标
print(kw.rect)

# tag_name 标签名
print(kw.tag_name)

# text 文本内容
print(kw.text)

news = driver.find_element(By.LINK_TEXT, '新闻')
print(news.text)
'''

'''
class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()

    # clear 清除输入内容
    def clear(self):
        self.driver.get('https://www.baidu.com')
        time.sleep(2)
        kw = self.driver.find_element(By.ID, 'kw')
        kw.send_keys('天气预报')

        time.sleep(2)
        kw.clear()

        time.sleep(2)
        kw.send_keys('热干面')

    # get_attribute 获取html属性
    def get_attribute(self):
        self.driver.get('https://www.baidu.com')
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.get_attribute('outerHTML'))
        print(kw.get_attribute('id'))
        print(kw.get_attribute('class'))
        print(kw.get_attribute('name'))
        print(kw.get_attribute('autocomplete'))

    # 判断标签是否在页面中显示，显示就是True， 不显示False
    def is_displayed(self):
        self.driver.get('https://www.baidu.com')
        ie = self.driver.find_element(By.XPATH, '//*[@id="form"]/input[1]')
        print(ie.is_displayed())
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.is_displayed())

    # is_enabled()判断元素是否可用, 可用返回True， 不可用返回False
    def is_enabled(self):
        self.driver.get('https://sahitest.com/demo/clicks.htm')
        btn1 = self.driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(16)')
        print(btn1.is_enabled())

        btn2 = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        print(btn2.is_enabled())

b = TestCase()
b.clear()
b.get_attribute()
b.is_enabled()
b.is_displayed()
'''
driver.quit()
